package com.jy.api.controller.pos;

import com.jy.api.dao.OrderItemDao;
import com.jy.api.dao.ProductTypeDao;
import com.jy.api.dao.StoreDao;
import com.jy.api.dao.WorkShiftDao;
import com.jy.api.domain.ProductType;
import com.jy.api.domain.Store;
import com.jy.api.domain.WorkShift;
import com.jy.api.service.PaymentService;
import com.jy.api.service.ProductTypeService;
import com.jy.api.vo.admin.payment.PaymentVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by lihu on 2018/1/16.
 */
@Slf4j
@RestController
@RequestMapping(value = "v1/pos", produces = "application/json;charset=utf-8")
public class PosCensus {


    @Autowired
    OrderItemDao orderItemDao;

    @Autowired
    ProductTypeService productTypeService;

    @Autowired
    PaymentService paymentService;

    @Autowired
    StoreDao storeDao;

    @Autowired
    ProductTypeDao productTypeDao;

    @Autowired
    WorkShiftDao workShiftDao;

    /**
     * 获取店铺所有商品分类信息
     *
     * @param storeId
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "product-types-all")
    public ResponseEntity getProductTypesAll(@RequestParam(name = "storeId") String storeId) {
        try {
            Store store = storeDao.findOne(storeId);
            if (store == null) {
                return new ResponseEntity("查询失败！店铺信息有误！", HttpStatus.BAD_REQUEST);
            }
            List<ProductType> productTypeList = new LinkedList<>();
            if (store.isSysProductEnabled()) {
                ProductType productType = productTypeDao.findOne("1");
                productTypeList.add(productType);
            }
            List<ProductType> productTypes = productTypeDao.findListByStoreIdAndEnabledOrderBySequenceAsc(
                    storeId, true);
            productTypeList.addAll(productTypes);
            return new ResponseEntity(productTypeList, HttpStatus.OK);
        } catch (Exception ex) {
            log.error("getProductTypes is error=>storeId:{}", storeId, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 获取时间段内商品销售信息
     *
     * @param storeId
     * @param from
     * @param to
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "{storeId}/product-sales")
    public ResponseEntity getSalesProduct(@PathVariable String storeId,
                                          @RequestParam(name = "productTypeId", defaultValue = "") String typeId,
                                          @RequestParam(name = "productId", defaultValue = "") String productId,
                                          @RequestParam(name = "from") String from,
                                          @RequestParam(name = "to") String to) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
            Date start = simpleDateFormat.parse(from);
            Date end = simpleDateFormat.parse(to);
            List<String> typeIds = new ArrayList<>();
            if (StringUtils.isNotBlank(typeId) && StringUtils.isBlank(productId)) {
                typeIds = productTypeService.getTypeIds(typeId);
            }
            List<Map<String, Object>> maps = orderItemDao.getProductTotal(storeId, typeIds, productId, start, end);
            for (Map<String, Object> map : maps) {
                map.put("amount", (Math.rint(Double.parseDouble(map.get("amount").toString()) * 100)) / 100d);
            }
            return ResponseEntity.ok(maps);
        } catch (Exception ex) {
            log.error("getOrderList is error=>storeId:{},from:{},to:{}", storeId, from, to, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);

        }

    }

    /**
     * 当班网费支付信息
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/{storeId}/cost-pay-total")
    public ResponseEntity getSalesTotal(@PathVariable String storeId) {
        try {

            WorkShift workShift = workShiftDao.findByStoreIdAndClosed(storeId, false);
            if (workShift == null) {
                return new ResponseEntity("查询失败！当前无开班信息", HttpStatus.BAD_REQUEST);
            }
            return ResponseEntity.ok(paymentService.getNetCostTotal(storeId, workShift.getBeginTime(), new Date()));
        } catch (Exception ex) {
            log.error("getSalesTotal is error=>storeId:{}", storeId, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

}
